package com.voidforce.activiti.listener;

import org.activiti.engine.RuntimeService;
import org.activiti.engine.TaskService;
import org.activiti.engine.delegate.Expression;
import org.activiti.engine.delegate.event.ActivitiEvent;
import org.activiti.engine.delegate.event.ActivitiEventListener;
import org.activiti.engine.delegate.event.impl.ActivitiEntityEventImpl;
import org.activiti.engine.impl.persistence.entity.TaskEntity;
import org.springframework.stereotype.Component;

/**
 * Created by wenjie.yang on 3/1/2019.
 */
@Component
public class MyEventListener implements ActivitiEventListener {

  private Expression fixedValue;

  private Expression dynamicValue;


  @Override
  public void onEvent(ActivitiEvent event) {
    if (!(event instanceof ActivitiEntityEventImpl)) {
      return;
    }
    ActivitiEntityEventImpl activitiEntityEventImpl = (ActivitiEntityEventImpl) event;
    Object entity = activitiEntityEventImpl.getEntity();
    if (!(entity instanceof TaskEntity)) {
      return;
    }
    TaskEntity taskEntity = (TaskEntity) entity;
    switch (event.getType()) {
      case TASK_ASSIGNED:
        System.out.println("task assign 了 我要给 "+ taskEntity.getAssignee()+"发邮件");
        break;
      default:
        break;
    }
  }

  /**
   * isFailOnException()方法决定了当事件分发时，onEvent(..)方法抛出异常时的行为。
   * 这里返回的是false，会忽略异常。 当返回true时，异常不会忽略，继续向上传播，迅速导致当前命令失败。
   * 当事件是一个API调用的一部分时（或其他事务性操作，比如job执行），
   * 事务就会回滚。当事件监听器中的行为不是业务性时，建议返回false。
   * @return
   */
  @Override
  public boolean isFailOnException() {
    return false;
  }


  public Expression getFixedValue() {
    return fixedValue;
  }

  public void setFixedValue(Expression fixedValue) {
    this.fixedValue = fixedValue;
  }

  public Expression getDynamicValue() {
    return dynamicValue;
  }

  public void setDynamicValue(Expression dynamicValue) {
    this.dynamicValue = dynamicValue;
  }
}
